package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.dao.TextbookDao;
import cn.ztydata.teachingsystem.heaven.entity.Textbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 教材服务类
 * Created by chenxin on 2014/12/10.
 */
@Service
public class TextbookService extends BaseService {
    @Autowired
    private TextbookDao textbookDao;

    @Autowired
    private TeacherTeamService teacherTeamService;

    @Autowired
    private WebContext webContext;

    /**
     * 新增课程教材
     * @param textbook 教材实体类
     *
     * @author cx
     * @since 2014-12-10
     */
    public void saveTextbook(Textbook textbook) {
        //判断用户是否在课程的教师团队
        teacherTeamService.isMemeber(textbook.getCourseId());

        if (!textbookDao.save(textbook)) {
            log.debug("新增教材失败:!textbookDao.save(textbook)");
            log.warn("新增教材失败:return false");
            throw new ServiceException();
        }
    }

    /**
     * 编辑课程教材
     * @param textbook 教材实体类
     *
     * @author cx
     * @since 2014-12-10
     */
    public void updateTextbook(Textbook textbook) {
        //判断用户是否在课程的教师团队
        teacherTeamService.isMemeber(textbook.getCourseId());

        if (!textbookDao.update(textbook)) {
            log.debug("编辑保存教材失败:!textbookDao.update(textbook)");
            log.warn("编辑保存教材失败:return false");
            throw new ServiceException();
        }
    }

    /**
     * 删除课程教材
     * @param id 教材ID
     *
     * @author cx
     * @since 2014-12-10
     */
    public void deleteTextbook(final long id, final long courseId) {
        //判断用户是否在课程的教师团队
        teacherTeamService.isMemeber(courseId);

        if (!textbookDao.delete(id)) {
            log.debug("删除教材失败:!textbookDao.delete(id)");
            log.warn("删除教材失败:return false");
            throw new ServiceException();
        }
    }

    /**
     * 获取指定课程教材集合
     * @param courseId 课程ID
     * @return List
     *
     * @author cx
     * @since 2014-12-10
     */
    public List<Textbook> getAll(final long courseId) {
        log.debug("获取指定课程教材集合");
        return textbookDao.getAll(courseId);
    }
}
